package com.wc.算法提高课.C第三章_图论.单源最短路的扩展应用.观光;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/10/12 14:49
 * @description https://www.acwing.com/problem/content/description/385/
 */
public class Main {
    /**
     * 思路：
     * 最小值在更新的时候会被更新为次小值
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 1010, M = 10010, INF = 0x3f3f3f3f;
    static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[M];
    static int[][] dist = new int[2][N], cnt = new int[2][N];
    static boolean[][] st = new boolean[2][N];
    // {dist[j][type], j, type},
    static PriorityQueue<int[]> q = new PriorityQueue<>(Comparator.comparingInt(o -> o[0]));
    static int n, m, idx = 1, S, T;

    public static void main(String[] args) {
        int cases = sc.nextInt();
        while (cases-- > 0) {
            n = sc.nextInt();
            m = sc.nextInt();
            idx = 1;
            Arrays.fill(h, 1, n + 1, 0);
            while (m-- > 0) {
                int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
                add(a, b, c);
            }
            S = sc.nextInt();
            T = sc.nextInt();
            dijkstra();
            out.println(cnt[0][T] + (dist[1][T] == (dist[0][T] + 1) ? cnt[1][T] : 0));
        }
        out.flush();
    }

    static void dijkstra() {
        Arrays.fill(dist[0], 1, n + 1, INF);
        Arrays.fill(dist[1], 1, n + 1, INF);
        Arrays.fill(st[0], 1, n + 1, false);
        Arrays.fill(st[1], 1, n + 1, false);
        Arrays.fill(cnt[0], 1, n + 1, 0);
        Arrays.fill(cnt[1], 1, n + 1, 0);
        dist[0][S] = 0;
        cnt[0][S] = 1;
        q.add(new int[]{dist[0][S], S, 0});
        while (!q.isEmpty()) {
            int[] node = q.poll();
            int t = node[1], type = node[2], distance = node[0], count = cnt[type][t];
            if (st[type][t]) continue;
            st[type][t] = true;
            for (int i = h[t]; i > 0; i = ne[i]) {
                int j = e[i];
                if (dist[0][j] > distance + w[i]) {
                    dist[1][j] = dist[0][j];
                    cnt[1][j] = cnt[0][j];
                    q.add(new int[]{dist[1][j], j, 1});
                    dist[0][j] = distance + w[i];
                    cnt[0][j] = count;
                    q.add(new int[]{dist[0][j], j, 0});
                } else if (dist[0][j] == distance + w[i]) {
                    cnt[0][j] += count;
                } else if (dist[1][j] > distance + w[i]) {
                    dist[1][j] = distance + w[i];
                    cnt[1][j] = count;
                    q.add(new int[]{dist[1][j], j, 1});
                } else if (dist[1][j] == distance + w[i]) {
                    cnt[1][j] += count;
                }
            }
        }
    }

    static void add(int a, int b, int c) {
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx++;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
